package com.yunji.product.service.impl;

import cn.hutool.core.lang.Pair;
import com.yunji.common.model.cache.ProductBelongCacheDTO;
import com.yunji.common.model.cache.ProductCacheDTO;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.bo.ProductListQueryBO;
import com.yunji.product.bo.ProductSelectQueryBO;
import com.yunji.product.dao.IProductDao;
import com.yunji.product.domain.ProductDO;
import com.yunji.product.service.IProductService;
import com.yunji.product.vo.ProductVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


/**
 * @Project: yunji-coffee
 * @Package com.yunji.product.service.impl
 * @FileName ProductBusiness.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * @Company com
 * @Version 1.0
 * @Since JDK1.8.0_201
 * @CreateTime 2024-04-01 23:38:08
 * @Description 商品信息接口实现
 * @Modification History
 * Author					History Time			Version			Description
 * -------------------------------------------------------------------------------
 * HuaAo				2024-06-10 23:38:08		1.0				1.0
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Resource
    private IProductDao productDao;

    @Override
    public List<ProductDO> searchProductPrice(ProductListQueryBO bo) {
        return productDao.searchProductPrice(bo);
    }

    @Override
    public ProductBelongCacheDTO selectProductPrice(ProductSelectQueryBO bo) {
        return productDao.selectProductPrice(bo);
    }


    /**
     * @param productId 商品ID
     * @Title select
     * @Desc 查询商品详情
     * @Date 2024-08-29 18:32:28.324
     * @Return Product 商品详情
     */
    @Override
    public ProductDO select(Long productId) {
        return productDao.getById(productId);
    }
//
//    @Override
//    public ProductDO getCacheById(Long productId) {
//        return productDao.getById(productId);
//    }

    /**
     * @return boolean 新增结果
     * @Title create
     * @Desc 新增商品信息
     * @Date 2024-04-08 09:45:32
     * @Param product 商品信息
     */
    @Override
    public boolean create(ProductDO product) {
        return productDao.save(product);
    }

    /**
     * @return boolean 更新结果
     * @Title update
     * @Desc 更新商品信息
     * @Date 2024-04-08 09:46:01
     * @Param product 商品信息
     */
    @Override
    public boolean update(ProductDO product) {
        return productDao.updateById(product);
    }

    /**
     * @return boolean 删除结果
     * @Title deleteProductByIds
     * @Desc 删除商品信息
     * @Date 2024-04-08 09:46:21
     * @Param ids 商品信息ID数组
     */
    @Override
    public boolean delete(Long... ids) {
        return productDao.removeByIds(Arrays.asList(ids));
    }

    /**
     * @param product 商品信息
     * @Title checkProductSKU
     * @Desc 校验商品SKU唯一
     * @Date 2024-06-11 17:34:59.308
     * @Return boolean 校验结果
     */
    @Override
    public boolean checkProductSKU(ProductVO product) {
        return productDao.checkProductSku(product) < 1;
    }

    /**
     * @param productTypeIds 产品类型ID数组
     * @Title statsOfProductType
     * @Desc 产品类型统计
     * @Date 2024-06-12 10:25:13.992
     * @Return int 统计结果
     */
    @Override
    public int statsOfProductType(Long... productTypeIds) {
        return productDao.statsOfProductType(productTypeIds);
    }

    @Override
    public int statsOfProductUnit(Long... unitIds) {
        return productDao.statsOfProductUnit(unitIds);
    }

    @Override
    public int statsOfProductBrand(Long... brandIds) {
        return productDao.statsOfProductBrand(brandIds);
    }

    @Override
    public Pair<String, String> getProductNameAndPics(Long productId) {
        return productDao.getProductNameAndPics(productId);
    }

    @Override
    public ProductCacheDTO getCacheById(Long productId) {
        ProductDO byId = productDao.getById(productId);
        if(byId!=null){
            ProductCacheDTO cache = new ProductCacheDTO();
            BeanUtils.clone(byId, cache);
            return cache;
        }
        return null;
    }

}
